home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / test_posix.py < prev    next >
Text File  |  2005-11-19  |  5KB  |  160 lines

  1. "Test posix functions"
  2.  
  3. from test import test_support
  4.  
  5. try:
  6.     import posix
  7. except ImportError:
  8.     raise test_support.TestSkipped, "posix is not available"
  9.  
  10. import time
  11. import os
  12. import sys
  13. import unittest
  14. import warnings
  15. warnings.filterwarnings('ignore', '.* potential security risk .*',
  16.                         RuntimeWarning)
  17.  
  18. class PosixTester(unittest.TestCase):
  19.  
  20.     def setUp(self):
  21.         # create empty file
  22.         fp = open(test_support.TESTFN, 'w+')
  23.         fp.close()
  24.  
  25.     def tearDown(self):
  26.         os.unlink(test_support.TESTFN)
  27.  
  28.     def testNoArgFunctions(self):
  29.         # test posix functions which take no arguments and have
  30.         # no side-effects which we need to cleanup (e.g., fork, wait, abort)
  31.         NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
  32.                              "times", "getloadavg", "tmpnam",
  33.                              "getegid", "geteuid", "getgid", "getgroups",
  34.                              "getpid", "getpgrp", "getppid", "getuid",
  35.                            ]
  36.  
  37.         for name in NO_ARG_FUNCTIONS:
  38.             posix_func = getattr(posix, name, None)
  39.             if posix_func is not None:
  40.                 posix_func()
  41.                 self.assertRaises(TypeError, posix_func, 1)
  42.  
  43.     def test_statvfs(self):
  44.         if hasattr(posix, 'statvfs'):
  45.             self.assert_(posix.statvfs(os.curdir))
  46.  
  47.     def test_fstatvfs(self):
  48.         if hasattr(posix, 'fstatvfs'):
  49.             fp = open(test_support.TESTFN)
  50.             try:
  51.                 self.assert_(posix.fstatvfs(fp.fileno()))
  52.             finally:
  53.                 fp.close()
  54.  
  55.     def test_ftruncate(self):
  56.         if hasattr(posix, 'ftruncate'):
  57.             fp = open(test_support.TESTFN, 'w+')
  58.             try:
  59.                 # we need to have some data to truncate
  60.                 fp.write('test')
  61.                 fp.flush()
  62.                 posix.ftruncate(fp.fileno(), 0)
  63.             finally:
  64.                 fp.close()
  65.  
  66.     def test_dup(self):
  67.         if hasattr(posix, 'dup'):
  68.             fp = open(test_support.TESTFN)
  69.             try:
  70.                 fd = posix.dup(fp.fileno())
  71.                 self.assert_(isinstance(fd, int))
  72.                 os.close(fd)
  73.             finally:
  74.                 fp.close()
  75.  
  76.     def test_dup2(self):
  77.         if hasattr(posix, 'dup2'):
  78.             fp1 = open(test_support.TESTFN)
  79.             fp2 = open(test_support.TESTFN)
  80.             try:
  81.                 posix.dup2(fp1.fileno(), fp2.fileno())
  82.             finally:
  83.                 fp1.close()
  84.                 fp2.close()
  85.  
  86.     def fdopen_helper(self, *args):
  87.         fd = os.open(test_support.TESTFN, os.O_RDONLY)
  88.         fp2 = posix.fdopen(fd, *args)
  89.         fp2.close()
  90.  
  91.     def test_fdopen(self):
  92.         if hasattr(posix, 'fdopen'):
  93.             self.fdopen_helper()
  94.             self.fdopen_helper('r')
  95.             self.fdopen_helper('r', 100)
  96.  
  97.     def test_fstat(self):
  98.         if hasattr(posix, 'fstat'):
  99.             fp = open(test_support.TESTFN)
  100.             try:
  101.                 self.assert_(posix.fstat(fp.fileno()))
  102.             finally:
  103.                 fp.close()
  104.  
  105.     def test_stat(self):
  106.         if hasattr(posix, 'stat'):
  107.             self.assert_(posix.stat(test_support.TESTFN))
  108.  
  109.     def test_chdir(self):
  110.         if hasattr(posix, 'chdir'):
  111.             posix.chdir(os.curdir)
  112.             self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
  113.  
  114.     def test_lsdir(self):
  115.         if hasattr(posix, 'lsdir'):
  116.             self.assert_(test_support.TESTFN in posix.lsdir(os.curdir))
  117.  
  118.     def test_access(self):
  119.         if hasattr(posix, 'access'):
  120.             self.assert_(posix.access(test_support.TESTFN, os.R_OK))
  121.  
  122.     def test_umask(self):
  123.         if hasattr(posix, 'umask'):
  124.             old_mask = posix.umask(0)
  125.             self.assert_(isinstance(old_mask, int))
  126.             posix.umask(old_mask)
  127.  
  128.     def test_strerror(self):
  129.         if hasattr(posix, 'strerror'):
  130.             self.assert_(posix.strerror(0))
  131.  
  132.     def test_pipe(self):
  133.         if hasattr(posix, 'pipe'):
  134.             reader, writer = posix.pipe()
  135.             os.close(reader)
  136.             os.close(writer)
  137.  
  138.     def test_tempnam(self):
  139.         if hasattr(posix, 'tempnam'):
  140.             self.assert_(posix.tempnam())
  141.             self.assert_(posix.tempnam(os.curdir))
  142.             self.assert_(posix.tempnam(os.curdir, 'blah'))
  143.  
  144.     def test_tmpfile(self):
  145.         if hasattr(posix, 'tmpfile'):
  146.             fp = posix.tmpfile()
  147.             fp.close()
  148.  
  149.     def test_utime(self):
  150.         if hasattr(posix, 'utime'):
  151.             now = time.time()
  152.             posix.utime(test_support.TESTFN, None)
  153.             posix.utime(test_support.TESTFN, (now, now))
  154.  
  155. def test_main():
  156.     test_support.run_unittest(PosixTester)
  157.  
  158. if __name__ == '__main__':
  159.     test_main()
  160.